//
//  SelectView.m
//  MindBox
//
//  Created by MS on 16-1-10.
//  Copyright (c) 2016年 Lemon. All rights reserved.
//

#define SLIP_VIEW_HEIGHT 1.5
#import "SelectView.h"
#import "UIView+Frame.h"
@interface SelectView ()
@property (nonatomic,weak) UIScrollView * scrollView;

@property (nonatomic,strong) UIButton * selectedBtn;
@property (nonatomic,assign) BOOL animation;
@end

@implementation SelectView

-(void)setItemsArray:(NSArray *)itemsArray
{
    _itemsArray = itemsArray;
    
    [self removeSubViews];
    [_btnArray removeAllObjects];
    
    int i = 0;
    for (Gift_Cat_Id * item in itemsArray) {
        
      UIButton * btn = [self setOneBtnWithName:item.name tag:i];
        
        if (i==0) {
            [self btnClick:btn];
        }
        i++;
    }
    
    [self layoutSubviews];
}


-(void)setButtonsArray:(NSArray *)buttonsArray
{
    _buttonsArray = buttonsArray;
    [self removeSubViews];
    [_btnArray removeAllObjects];
    
    int i = 0;
    for (NSString * title in buttonsArray) {
        
        UIButton * btn = [self setOneBtnWithName:title tag:i];
        
        if (i==0) {
            [self btnClick:btn];
        }
        i++;
    }
    
    [self layoutSubviews];
}

-(void)removeSubViews
{
    for (UIView * view in self.scrollView.subviews) {
        if ([view isKindOfClass:[UIButton class]]) {
            [view removeFromSuperview];
        }
    }
}


-(UIButton *)setOneBtnWithName:(NSString *)title tag:(NSInteger)tag
{
    UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setAdjustsImageWhenHighlighted:NO];
    btn.tag = tag;
    
    btn.titleLabel.font = [UIFont systemFontOfSize:11];
    [btn setTitle:title forState:UIControlStateNormal];
    
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
    [self.scrollView addSubview:btn];
    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [btn sizeToFit];
    [self.btnArray addObject:btn];
    return btn;
}


-(void)btnClick:(UIButton *)btn
{
   
    self.selectedBtn.selected = NO;
    btn.selected = YES;
    
    self.selectedBtn = btn;
    
    if (self.animation) {
        [UIView animateWithDuration:0.25 animations:^{
            self.slipView.frame = CGRectMake(btn.x, self.height-SLIP_VIEW_HEIGHT, btn.width, SLIP_VIEW_HEIGHT);
        }];
    }
    
    else{
        
        self.slipView.frame = CGRectMake(btn.x, self.height-SLIP_VIEW_HEIGHT, btn.width, SLIP_VIEW_HEIGHT);
    }
    
    self.animation = YES;
    
    if ([self.delegate respondsToSelector:@selector(selectViewDidClick:index:)]) {
        [self.delegate selectViewDidClick:self index:btn.tag];
    }
}


-(void)selectIndex:(NSInteger)index animation:(BOOL)animation
{
    self.animation = animation;
    
    [self btnClick: self.btnArray[index]];
}


-(NSMutableArray *)btnArray
{
    if (_btnArray==nil) {
        _btnArray = [NSMutableArray array];
        
    }
    return _btnArray;
}

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor =[UIColor whiteColor];
        UIScrollView * scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
        [self addSubview:scrollView];
        self.scrollView = scrollView;
        scrollView.contentInset = UIEdgeInsetsMake(0, 10, 0, 10);
        scrollView.contentOffset = CGPointMake(-10, 0);
        scrollView.showsHorizontalScrollIndicator = NO;
   
        UIView *slipView = [[UIView alloc] init];
        [scrollView addSubview:slipView];
        slipView.backgroundColor = [UIColor redColor];
        self.slipView = slipView;
        self.animation = YES;
    }
    return self;
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    
    self.scrollView.frame = self.bounds;
    
    int i = 0;
    CGFloat x = 0;
    for (UIButton * btn in self.btnArray) {
        CGFloat margin ;
        if (_btnArray.count>=5) {
            margin = 20;
        }
        else{
            margin = 45;
        }
        
        btn.x = x;
        x = x + btn.width+margin;
        btn.y = (self.height-btn.height)*0.5;
        
        if (i==0) {
            self.slipView.frame = CGRectMake(btn.x, self.height-SLIP_VIEW_HEIGHT, btn.width, SLIP_VIEW_HEIGHT);
        }
        
        if (i==self.btnArray.count-1) {
            self.scrollView.contentSize = CGSizeMake(CGRectGetMaxX(btn.frame), 0);
        }
       
        i++;
    }
    
   
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
